#ifndef XGLOG_H_
#define XGLOG_H_
#include <memory>
#include <string>
#include <map>
#include "Global.h"

typedef enum {
	CL_DEF = 0,
	CL_GREEN,
	CL_BLUE,
	CL_RED,
	CL_YELLOW,
	CL_PURPLE
}COLOR_S;

typedef enum {
	CP_MAIN,
	CP_SUB
}COLOR_PLAN;

typedef struct
{
	int log_level;
	char *log_head;
	int color;
	bool update_to_db;
}UI_INFO_S;

class xglog
{
public:
	~xglog();
	static std::shared_ptr<xglog> GetInstance()
	{
		if (p_this_ == nullptr)
		{
			return p_this_ = std::shared_ptr<xglog>(new xglog);
		}
		else
			return p_this_;
	}

	void init(std::string log_path);

	void write_to_file(std::string path, std::string str, int color_plan = CP_MAIN);

	void write_to_file(std::string path, CString str, int color_plan = CP_MAIN)
	{
		write_to_file(path, cstringTostring(str), color_plan);
	}

	void write_to_file_interval(std::string path, std::string log)
	{
		auto  it = interval_log_.find(log);
		if (it == interval_log_.end())
		{
			interval_log_[log] = GetTickCount();
			write_to_file(path, log);
		}
		else
		{
			auto interval = GetTickCount() - it->second;

			//Brush every five minutes
			if (interval < 1000 * 60*5)
				return;
			else
			{
				write_to_file(path, log);
				interval_log_.erase(it);
			}
		}
	}

	void write_to_file_interval(std::string path, CString log)
	{
		write_to_file_interval(path, cstringTostring(log));
	}


private:
	xglog();

	static std::shared_ptr<xglog> p_this_;

	std::string pre_log_;

	std::map<std::string, unsigned long> interval_log_;


	UI_INFO_S *ui_infos_default_;
	UI_INFO_S *ui_infos_a_plan;
};

#define NAV_FILE std::string(__FILE__)+std::to_string(__LINE__)

#define XM_LOG(x) xglog::GetInstance()->write_to_file(NAV_FILE,x)

#define XM_LOG_COLOR_A(x) xglog::GetInstance()->write_to_file(NAV_FILE,x,CP_SUB)

#define XM_LOG_ONCE(x)	xglog::GetInstance()->write_to_file_interval(NAV_FILE,x)


#endif